import jieba
import codecs,sys
import wordcloud
f = codecs.open('西游记.txt','r','UTF-8')
b_text=f.read()
f.close()
a = jieba.lcut(b_text)
dic = {}
for word in a:
    if word not in dic:
        dic[word]=1
    else:
        dic[word]+=1
del dic['，']
del dic['。']
del dic['、']
del dic['；']
del dic['“']
del dic['”']
swd=sorted(list(dic.items()),key=lambda lst:lst[1],reverse=1)
for kword,times in swd:
   print(kword,times)
print(type(swd))
w = wordcloud.WordCloud(font_path='msyh.ttc', height=700, width=1000, background_color='white')
w.generate(" ".join(a))
w.to_file('词云图.png')
